﻿//
// PolyLineGraphEditPanel.xaml.cpp
// Implementation of the PolyLineGraphEditPanel class
//

#include "pch.h"
#include "PolyLineGraphEditPanel.xaml.h"

using namespace PolyLineGraph2;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

PolyLineGraphEditPanel::PolyLineGraphEditPanel(BooPolyLineGraph^ m_linegraph)
{
	InitializeComponent();
	linegraph = m_linegraph;
}

void PolyLineGraphEditPanel::ApplyData(Platform::Object^ data)
{
	PolyLineGraphData^ lineDatas = (PolyLineGraphData^) data;
	firstRowNameText->Text = lineDatas->rowNames->GetAt(0);
	secondRowNameText->Text = lineDatas->rowNames->GetAt(1);

	StackPanel^ singleColumnDataPanel;
	lineDataPanel->Children->Clear();
	inputPanelDatas.clear();
	
	for(unsigned int i = 0; i < lineDatas->columeNames->Size; i++)
	{
		singleColumnDataPanel = ref new StackPanel();

		TextBox^ columnNameText = ref new TextBox();
		columnNameText->Text = lineDatas->columeNames->GetAt(i);
		singleColumnDataPanel->Children->Append(columnNameText);

		TextBox^ columnValueText1 = ref new TextBox();
		columnValueText1->Text = lineDatas->pointValues1->GetAt(i) + "";
		singleColumnDataPanel->Children->Append(columnValueText1);

		TextBox^ columnValueText2 = ref new TextBox();
		columnValueText2->Text = lineDatas->pointValues2->GetAt(i) + "";
		singleColumnDataPanel->Children->Append(columnValueText2);

		Button^ deleteButton = ref new Button();
		deleteButton->Content = "删除";
		singleColumnDataPanel->Children->Append(deleteButton);

		lineDataPanel->Children->Append(singleColumnDataPanel);
		inputPanelDatas.push_back(singleColumnDataPanel);
	}
}

void PolyLineGraphEditPanel::dataContentSizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e)
{
	
}

void PolyLineGraph2::PolyLineGraphEditPanel::OKButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	//get the output data
	PolyLineGraphData^ outDatas = ref new PolyLineGraphData();
	String^ tmp = firstRowNameText->Text;
	
	outDatas->addRow(tmp,  Windows::UI::Colors::AliceBlue);
	outDatas->addRow(tmp, Windows::UI::Colors::Blue);
	for(unsigned int i = 0; i < inputPanelDatas.size(); i++)
	{
		TextBox^ firstTextBox = (TextBox^)(((StackPanel^)lineDataPanel->Children->GetAt(i))->Children->GetAt(1));
		TextBox^ secondTextBox = (TextBox^)(((StackPanel^)lineDataPanel->Children->GetAt(i))->Children->GetAt(2));

		TextBox^ columnNameTextBox = (TextBox^)(((StackPanel^)lineDataPanel->Children->GetAt(i))->Children->GetAt(0));
		double val1 = _wtof(firstTextBox->Text->Data());
		double val2 = _wtof(secondTextBox->Text->Data());
		ListCollection<double>^  testDatas = ref new ListCollection<double>;
		testDatas->Append(val1);
		testDatas->Append(val2);
		outDatas->addColume(columnNameTextBox->Text, testDatas);
	}


	linegraph->RePaint(outDatas);
}
